
$(function () {
    //关闭顶部广告
    $(".banner-top-close").on("click", function () {
        console.log("关闭顶部广告");
        $(".banner-top").slideUp(200);
    })

    $(".shortcut-ul .adress").hover(function () {
        $(".shortcut-ul .adress-sub").show();
    }, function () {
        $(".shortcut-ul .adress-sub").hide();
    })
    $(".shortcut-ul .adress-sub ul li").hover(function () {
        if ($(this).find("a").text() != $(".adress-name").text()) {
            $(this).css("backgroundColor", "rgb(204, 204, 204,.3)").find("a").css("color", "#e33333");
        }

    }, function () {
        if ($(this).find("a").text() != $(".adress-name").text()) {
            $(this).css("backgroundColor", "#fff").find("a").css("color", "#999");
        }
    })
    $(".adress-sub").on("click", "li", function () {
        $(".adress-sub li").css("backgroundColor", "#fff").find("a").css("color", "#999");
        $(this).css("backgroundColor", "#e33333").find("a").css("color", "#fff");
        var adressName = $(this).text();
        // console.log(adressName);
        $(".adress-name").text(adressName);
    })
    var flag = false;
    $(".header-logo").on("mouseenter", function () {
        if (!flag) {
            var logoSub = document.createElement("div");
            $(logoSub).addClass("header-logo-sub");
            if ($(this).children().length == 0) {
                $(this).css("visibility", "hidden");
                $(this).before(logoSub);
                var t = setTimeout(function () {
                    $(".header-logo-sub").remove();
                    $(".header-logo").css("visibility", "visible");
                    flag = true;
                }, 5000);
            }
        }
    })
    $(".act-banner").stop().hover(function () {
        $(".main .menu .act-banner-r").css("width", "790px");
    }, function () {
        $(".main .menu .act-banner-r").css("width", "0");
    })

    // 京东秒杀倒计时
    function countdown() {
        var date = new Date();
        var hours = date.getHours();
        if (hours >= 22) {
            $(".countdown-main-h").text("00");
            $(".countdown-main-m").text("00");
            $(".countdown-main-s").text("00");
        } else {
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            var dateStr = "2020 " + String(date.getMonth() + 1) + "-" + date.getDate() + " 22:00:00";
            var date2 = new Date(dateStr);
            // console.log(dateStr);
            var times = date2.getTime() - date.getTime();
            times = parseInt(times) / 1000;
            var hour = parseInt(times / 60 / 60 % 24);
            hour = hour < 10 ? '0' + hour : hour;
            var minute = parseInt(times / 60 % 60);
            minute = minute < 10 ? '0' + minute : minute;
            var second = parseInt(times % 60);
            second = second < 10 ? '0' + second : second;
            // console.log(hour + " " + minute + " " + second);
            $(".countdown-main-h").text(hour);
            $(".countdown-main-m").text(minute);
            $(".countdown-main-s").text(second);
        }

    }
    setInterval(countdown, 1000);
    // 侧边栏
    var elevator_li = document.createElement("li");
    elevator_li.className = 'last_li';
    $(elevator_li).html('<a href="Javascript: void (0); "><i>&#xe66d;</i>顶部</a>');
    // <a href="Javascript:void(0);"><i>&#xe640;</i>反馈</a>
    $(document).on("scroll", function () {
        if (window.pageYOffset >= 680) {
            $(".elevator").css("position", "fixed");
            $(".elevator").css("top", "60px").find("ul").append(elevator_li);
            $(".last_li").on("click", function () {
                animate2(window, 0);
            })
        } else {
            $(".elevator").css("position", "absolute");
            $(".elevator").css("top", "770px");
            $(".elevator").find(".last_li").remove();
        }
    })
    //返回顶部动画
    function animate2(obj, target, callback) {
        //先清除其他的定时器
        clearInterval(obj.timer);
        /*用obj.time可以利用给对象添加属性的性质来节省内存*/
        obj.timer = setInterval(function () {
            //涉及到像素应该尽量避免小数
            //向上取整 Math.ceil   向下取整 Math.floor
            var step = (target - obj.pageYOffset) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (obj.pageYOffset == target) {
                clearInterval(obj.timer);
                if (callback) {
                    callback();
                }
            }
            window.scroll(0, obj.pageYOffset + step);
        }, 3)
    }
    //返回京东秒杀
    console.log($(".elevator ul li").eq(0));
    $(".elevator ul li").eq(0).on("click", function () {
        seckillTop = document.querySelector(".seckill").offsetTop;
        // console.log(seckillTop);
        animate2(window, seckillTop)
    })
})